package cn.edu.neusoft.wuguangsheng.flowerimpression.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.squareup.picasso.Picasso;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import cn.edu.neusoft.wuguangsheng.flowerimpression.R;
import cn.edu.neusoft.wuguangsheng.flowerimpression.Utils.TimeUtils;
import cn.edu.neusoft.wuguangsheng.flowerimpression.activity.OtherUserInfoActivity;
import cn.edu.neusoft.wuguangsheng.flowerimpression.bean.AnswerBean;
import cn.edu.neusoft.wuguangsheng.flowerimpression.service.Service;

public class AnswerAdapter extends BaseAdapter<AnswerBean> {

    String times;

    public AnswerAdapter(Context context, List<AnswerBean> items, int layoutResourse) {
        super(context, items, layoutResourse);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView= LayoutInflater.from(context).inflate(layoutResourse,parent,false);
        AnswerViewHolder viewHolder = new AnswerViewHolder(itemView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        AnswerViewHolder viewHolder=(AnswerViewHolder) holder;
        AnswerBean answerBean=items.get(position);

        final String username =answerBean.getUser_nickname();
        final String time = answerBean.getAnswer_time();
        final String content=answerBean.getAnswer_content();
        final String sex = answerBean.getUser_sex();
        final String userid = answerBean.getUser_id();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date = simpleDateFormat.parse(time);
            times = TimeUtils.getTimeFormatText(date);
        }catch (Exception e){
            e.printStackTrace();
        }

        viewHolder.answeritem_username.setText(username);
        viewHolder.answeritem_time.setText(times);
        viewHolder.answeritem_content.setText(content);

        if (answerBean.getUser_photo().equals("1") || answerBean.getUser_photo().equals("1.jpg")){
            if (sex.equals("1")){
                viewHolder.answeritem_img.setImageResource(R.mipmap.nanshi);
            }
            else{
                viewHolder.answeritem_img.setImageResource(R.mipmap.nvshi);
            }
        }
        else{
            String img = Service.BASE_URL+"flower_impression/public/uploads/"+answerBean.getUser_photo();
            Picasso.with(context).load(img).into(viewHolder.answeritem_img);
        }

        viewHolder.answeritem_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, OtherUserInfoActivity.class);
                intent.putExtra("userid",userid);
                context.startActivity(intent);
            }
        });
    }
}
